package org.example;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.LinkedList;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class PeopleEntity implements Comparable<PeopleEntity> {
    /**
     * 序号
     */
    private Integer seq;
    /**
     * 姓名
     */
    private String name;
    /**
     * 性别
     */
    private String sex;
    /**
     * 年龄
     */
    private Integer age;
    /**
     * 城市
     */
    private String city;
    /**
     * 存款
     */
    private BigDecimal deposit;
    /**
     * 最终存款格式
     */
    private String finalDeposit;
    /**
     * 存款时间
     */
    private String depositTime;
    /**
     * 第几天
     */
    private Integer currentDay;

    @Override
    public int compareTo(PeopleEntity o) {
        return o.deposit.compareTo(this.deposit);
    }

    public String wrapper(String deposit) {
        StringBuilder wrapperDeposit=new StringBuilder();
        LinkedList<Character> list = new LinkedList<>();
        String[] split = deposit.split("\\.");
        // 小数点左边
        String left = split[0];
        // 小数点右边
        String right = split[1];
        int cnt = 1;
        char[] charArray = left.toCharArray();
        for (int i = charArray.length - 1; i >= 0; i--) {
            // 每3个字符添加一个逗号除了最高位
            if (cnt % 3 == 0 && i != 0) {
                list.addFirst(charArray[i]);
                list.addFirst(',');
            } else {
                list.addFirst(charArray[i]);
            }
            cnt++;
        }
        for (Character character : list) {
            wrapperDeposit.append(character);
        }

        return wrapperDeposit+ "." + right;
    }

    @Override
    public String toString() {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(this.getSeq()).append("|").append(this.getName()).append("|").append(this.getSex()).append("|").append(this.getAge()).append("|").append(this.getCity()).append("|").append(wrapper(this.getFinalDeposit())).append(" 元|").append(this.getDepositTime()).append("|").append(this.getCurrentDay());
        return stringBuilder.toString();
    }
}
